package com.southwind.controller;


import com.southwind.common.ResultObj;
import com.southwind.entity.RedisConstants;
import com.southwind.util.AliOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class UploadController {

    //MultipartFile封装图片上传的所有信息
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/upload")
    public ResultObj upload(MultipartFile file){
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //1.png
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String newFileName=uuid+extend;
        String url= "";
        try {
             url = AliOSSUtil.uploadFile(newFileName, file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE,url);
        return new ResultObj(url,0,"上传成功");
//        return Result.ok("上传成功",url);
    }

    public static void main(String[] args) {
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid.replaceAll("-",""));
    }
}
